package com.ukao.cashregister.ui.vipManager;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.mcxtzhang.commonadapter.rv.OnItemClickListener;
import com.ukao.cashregister.R;
import com.ukao.cashregister.adapter.VipLeverAdapter;
import com.ukao.cashregister.adapter.VipListAdapter;
import com.ukao.cashregister.base.IAdapter;
import com.ukao.cashregister.base.IPage;
import com.ukao.cashregister.base.MvpFragment;
import com.ukao.cashregister.base.PageWrapper;
import com.ukao.cashregister.bean.RechargeBean;
import com.ukao.cashregister.bean.UserInfoBean;
import com.ukao.cashregister.bean.VipLeverBean;
import com.ukao.cashregister.consts.GlobalConsts;
import com.ukao.cashregister.listener.MyOnItemClickListener;
import com.ukao.cashregister.listener.OnItemChildClickListener;
import com.ukao.cashregister.pesenter.VipTablePesenter;
import com.ukao.cashregister.ui.createOrders.UserInfoFragment;
import com.ukao.cashregister.ui.stokin.GatheringFragment;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.view.VipTableView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class VipTableFragment extends MvpFragment<VipTablePesenter>  implements VipTableView{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.level_recycler_view)
    RecyclerView levelRecyclerView;
    @BindView(R.id.vip_recycler_view)
    RecyclerView vipRecyclerView;
    @BindView(R.id.twinkling_layout)
    TwinklingRefreshLayout twinklingLayout;
    Unbinder unbinder;


    private  List<RechargeBean> mList;
    private ArrayList<VipLeverBean.ListBean>   mLeverData;
    private VipLeverAdapter mVipLeverAdapter;
    private VipListAdapter mVipListAdapter;
    private PageWrapper mPageWrapper;
    private List<UserInfoBean.UserBean> items = new ArrayList<>();
    private String groupId;
    private  String  cardId="";
    private   String    searchValue="";
    public UserInfoBean.UserBean mVipListitem;
    public String userId;
    private String amount;

    private int  checkItemPos;

    public static VipTableFragment newInstance(String param1, ArrayList<VipLeverBean.ListBean>   mLeverData) {
        VipTableFragment fragment = new VipTableFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putParcelableArrayList(ARG_PARAM2, mLeverData);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            groupId= getArguments().getString(ARG_PARAM1);
            mLeverData=getArguments().getParcelableArrayList(ARG_PARAM2);

        }
    }

    @Override
    protected VipTablePesenter createPresenter() {
        return new VipTablePesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_vip_table, container, false);
        unbinder = ButterKnife.bind(this, view);


        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }

    public  void  setSearchValue(String  searchValue){
        this.searchValue=searchValue;
        twinklingLayout.startRefresh();

    }

    public  void  setnullSearchValue(String  searchValue){
            this.searchValue=searchValue;


    }

    @Override
    protected void initView() {
         List<VipLeverBean.ListBean> mUseLeverData=new ArrayList<>();
        if (!CheckUtils.isEmpty(mLeverData)){
            VipLeverBean.ListBean mitem = mLeverData.get(0);
            cardId=mitem.getId();
            mitem.setCheck(true);
            checkItemPos=0;
            mUseLeverData.addAll(mLeverData);
            for (int i = 0; i < mLeverData.size(); i++) {
                    if (i!=0){
                        mLeverData.get(i).setCheck(false);
                    }
            }

        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        levelRecyclerView.setLayoutManager(linearLayoutManager);
        mVipLeverAdapter=new VipLeverAdapter(activity,mUseLeverData,true);
        levelRecyclerView.setAdapter(mVipLeverAdapter);
        initLinearRecyclerView(vipRecyclerView);
        mVipListAdapter=new VipListAdapter(activity,items);
        vipRecyclerView.setAdapter(mVipListAdapter);
              IPage page = new IPage() {


            @Override
            public void load(int geIndex, int pageSize) {

                mvpPresenter.getViPTabData(_mActivity,geIndex,pageSize,groupId,cardId,searchValue);

            }
        };

        mPageWrapper= new PageWrapper<>(mIAdapter, page);

    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (!CheckUtils.isEmpty(mLeverData)){
            mLeverData.get(checkItemPos).setCheck(true);
            for (int i = 0; i < mLeverData.size(); i++) {
                VipLeverBean.ListBean listBean = mLeverData.get(i);
                if (checkItemPos!=i){
                    listBean.setCheck(false);
                }
                if (listBean.isCheck()){
                    L.i("pos="+i);
                }



            }
        }



        twinklingLayout.startRefresh();

    }

    IAdapter mIAdapter = new IAdapter() {
        @Override
        public void setDataSource(List data, boolean isNextLoad) {

            mVipListAdapter.setDatas(data);
            twinklingLayout.finishRefreshing();

            twinklingLayout.setEnableLoadmore(isNextLoad);


        }

        @Override
        public void loadMoreFinish(List data, boolean isNextLoad) {
            mVipListAdapter.addDatas(data);
            twinklingLayout.finishLoadmore();
            twinklingLayout.setEnableLoadmore(isNextLoad);

        }
    };

    @Override
    protected void initListener() {
        super.initListener();
        mVipLeverAdapter.setOnItemClickListener(mOnItemClickListener);
        mVipListAdapter.setOnItemClickListener(mOnItemClickListener);
        twinklingLayout.setOnRefreshListener(mRefreshListenerAdapter);
        mVipListAdapter.setOnItemChildClickListener(mOnItemChild);


    }

    private OnItemChildClickListener mOnItemChild=new OnItemChildClickListener() {


        @Override
        public void onChildItemClick(int id, int pos) {
            mVipListitem= mVipListAdapter.getItem(pos);
            if (mVipListitem==null)return;
            switch (id){

                case  R.id.recharge_btn:

                    userId=mVipListitem.getInfo().getId();
                        if (mList==null){
                            mvpPresenter.queryRechargeList(_mActivity);
                        }else{
                            if (getParentFragment() instanceof VipManagerFragment) {

                                ((VipManagerFragment) getParentFragment()).start(GatheringFragment.newInstance((ArrayList<RechargeBean>) mList,mVipListitem));
                            }


                           // showRechargeDialog();
                        }


                    break;
                case   R.id.place_order_btn :/*下单*/
                    if (getParentFragment() instanceof VipManagerFragment) {

                        ((VipManagerFragment) getParentFragment()).start(UserInfoFragment.newInstance(GlobalConsts.CREATE_ORDERS,mVipListitem,"会员管理"));
                    }

                break;



            }

        }
    };









    private RefreshListenerAdapter mRefreshListenerAdapter=new RefreshListenerAdapter() {
        @Override
        public void onRefresh(TwinklingRefreshLayout mtwinklingLayout) {


            mPageWrapper.loadPage(true);
        }

        @Override
        public void onLoadMore(TwinklingRefreshLayout mtwinklingLayout) {

            mPageWrapper.loadPage(false);

        }
    };



    private OnItemClickListener  mOnItemClickListener=new MyOnItemClickListener(){


        @Override
        public void onItemClick(ViewGroup viewGroup, View view, Object o, int i) {
            super.onItemClick(viewGroup, view, o, i);
            if (viewGroup==levelRecyclerView){

                VipLeverBean.ListBean item = (VipLeverBean.ListBean) o;
            if (!item.isCheck()){
                checkItemPos=i;
                item.setCheck(!item.isCheck());
                List<VipLeverBean.ListBean>    mData=mVipLeverAdapter.getDatas();
                for (int j = 0; j <mData.size(); j++) {
                    if (i!=j){
                        mData.get(j).setCheck(false);

                    }
                }
                cardId=item.getId();
                twinklingLayout.startRefresh();
                mVipLeverAdapter.notifyDataSetChanged();

            }
            }else if (viewGroup==vipRecyclerView){
                if (getParentFragment() instanceof VipManagerFragment) {

                    Log.d("会员管理", "onItemClick: ");
                    UserInfoBean.UserBean item = mVipListAdapter.getItem(i);
                    ((VipManagerFragment) getParentFragment()).start(UserInfoFragment.newInstance(GlobalConsts.VIP_DETAILS,item,"会员管理"));
                }

            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void loadFault(String message) {
        T.show(message);
    }

    @Override
    public void loadVipTableSucceed(List<UserInfoBean.UserBean> mList, int total) {

        mPageWrapper.addDataSource(mList, total);
    }

    @Override
    public void rechargeTableSucceed(List<RechargeBean> mList) {
        this.mList=mList;
      //  showRechargeDialog();
        if (getParentFragment() instanceof VipManagerFragment) {

            ((VipManagerFragment) getParentFragment()).start(GatheringFragment.newInstance((ArrayList<RechargeBean>) mList,mVipListitem));
        }
    }

    @Override
    public void rechargeSucceed(String msg) {

        twinklingLayout.startRefresh();
        T.show(msg);

    }


}
